package org.gwtEss.other;

import java.util.ArrayList;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class NewPortletContainer extends SplitLayoutPanel{

	ArrayList<NewPortletContainer> portletCols = new ArrayList<NewPortletContainer>();; 
	DialogBox db = new DialogBox(true);
	SimplePanel dbPanel = new SimplePanel();
	public void addInColumn(int colNum, final Widget widget){
		int size = portletCols.size(); 
		NewPortletContainer portletContainer = null;
		if (portletCols.size() == colNum){
			portletCols.add(portletContainer = new NewPortletContainer());
			addWest(portletContainer, 200);
		}else if (colNum >= portletCols.size())
			assert false:"wrong colNum";
		else 
			portletContainer = portletCols.get(colNum);
		db.setText("header");
		db.setWidget(dbPanel);
		final HorizontalPanel hp = new HorizontalPanel(){
			{
				add(widget);setSize("100%", "100%");
				addStyleName("cccccc");
				addMouseMoveHandler(new MouseMoveHandler() {
					
					@Override
					public void onMouseMove(MouseMoveEvent event) {
						dbPanel.setWidget(widget);
						db.show();
						db.setPopupPosition(getAbsoluteLeft()-5, getAbsoluteTop() - 25);
						db.setWidth(getOffsetWidth() + 10 + "px");
						db.setHeight(getOffsetHeight() + 35 + "px");
					}
				});
			}
		};
		db.addCloseHandler(new CloseHandler<PopupPanel>() {
			
			@Override
			public void onClose(CloseEvent<PopupPanel> event) {
				hp.add(widget);
			}
		});
		portletContainer.addNorth(hp, 200);
		
/*		portletContainer.addNorth(new DockLayoutPanel(Unit.PX){
			int getLeft(){return getAbsoluteLeft();}
			int getTop(){return getAbsoluteTop();}
			{
				Label header;
				addNorth(header = new Label("header"), 20);
				new Draggable(header){
					private int left;
					private int top;

					protected boolean isPreventDefault(int x, int y) {return true;}

					@Override
					public void onDrag(int mouseX, int mouseY, int deltaX,
							int deltaY) {
						System.out.println("dragged");
//						setLeft(left - deltaX);
//						setTop(top-deltaY);
					}

					@Override
					public void onDragEnd(int mouseX, int mouseY, int deltaX,
							int deltaY) {
						
					}

					@Override
					public void onDragStart(int mouseX, int mouseY) {
						left = getLeft();
						top = getTop(); 
						
						db.show();
						db.setWidget(widget);
						db.setPopupPosition(left-5, top);
						System.out.println("left: " + left + "top: " + top);
					}};
				
				add(widget);
			}
		}, 100);*/
	}
	public NewPortletContainer() {}
	
}
